package com.sf.iisp.bind.action;

import java.util.Collection;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.sf.iisp.BindingException;
import com.sf.iisp.bind.biz.BindingBiz;
import com.sf.iisp.bind.biz.UnbindingBiz;
import com.sf.iisp.bind.domain.BindingRecord;
import com.sf.iisp.kafka.adapter.IKafkaProducerPoolDelegate;
import com.sf.novatar.base.action.ActionDef;
import com.sf.novatar.base.action.ActionType;
import com.sf.novatar.base.action.BaseAction;
import com.sf.novatar.base.dao.IPage;

@ActionDef(type = ActionType.PRIVATE)
@Controller
@Scope("prototype")
public class BindAction extends BaseAction {

    private Collection<BindingRecord> rows;

    private int total;

    private String jsons;

    private String ids;

    @Qualifier("producer")
    @Resource
    private IKafkaProducerPoolDelegate delegate;

    @Resource
    private BindingBiz bindingBiz;

    @Resource
    private UnbindingBiz unbindingBiz;

    private static final long serialVersionUID = 1L;

    public String findBindRecordMain() {
        return SUCCESS;
    }


    public String findPageByBr() {
        IPage<BindingRecord> page = bindingBiz.findPageBindingRecord(createRequestParams());
        rows = page.getData();
        total = page.getTotalSize();
        return SUCCESS;
    }

    public String bind() throws BindingException {
        HttpServletRequest request = ServletActionContext.getRequest();
        jsons = request.getParameter("jsons");
        msg = bindingBiz.bind(jsons);
        return SUCCESS;
    }

    public String unbind() throws BindingException {
        HttpServletRequest request = ServletActionContext.getRequest();
        jsons = request.getParameter("jsons");
        msg = unbindingBiz.unbind(jsons);
        return SUCCESS;
    }

    public String sendKafka() throws BindingException {
        HttpServletRequest request = ServletActionContext.getRequest();
        jsons = request.getParameter("jsons");
        delegate.sendString(jsons);
        return SUCCESS;
    }


    public String unbindByIds() throws BindingException {
        if (ids != null && ids.length() > 0) {
            String[] idsArr = ids.split(",");
            for (int i = 0; i < idsArr.length; i++) {
                BindingRecord bindingRecord = bindingBiz.loadById(idsArr[i]);
                @SuppressWarnings("unused")
                String unbindResult = unbindingBiz.unbind(bindingRecord);
            }
        }
        return SUCCESS;
    }


    public Collection<BindingRecord> getRows() {
        return rows;
    }


    public void setRows(Collection<BindingRecord> rows) {
        this.rows = rows;
    }


    public int getTotal() {
        return total;
    }


    public String getJsons() {
        return jsons;
    }


    public void setJsons(String jsons) {
        this.jsons = jsons;
    }

    public String getIds() {
        return ids;
    }


    public void setIds(String ids) {
        this.ids = ids;
    }
}











